﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using SinsenWeb.Helpers;
using SinsenWeb.Models;

namespace SinsenWeb.Controllers
{
    public class LoginController: BaseController
    {

        public ActionResult Index()
        {
            
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult LogIn(string username, string password)
        {
            if(Membership.Provider.ValidateUser(username, password))
            {
                FormsAuthentication.SetAuthCookie(username, true);

                MessageHelper.Message("Velkommen");

                return PerformDefaultRedirect(Request.Form["ReturnUrl"]);
            }
            else
            {
                MessageHelper.Alert("Brukernavn eller passord feil");
                return RedirectToAction("Index");
            }
        }

        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            return PerformDefaultRedirect(Request.Params["ReturnUrl"]);
        }

        private ActionResult PerformDefaultRedirect(string returnUrl)
        {
            if (!string.IsNullOrEmpty(returnUrl))
            {
                return Redirect(returnUrl);
            }
            return RedirectToAction("Index", "Home");

        }
    }
}
